/**
 * NavUri 0.0.2
 *
 * https://code.google.com/js-navuri
 *
 * Copyright (c) 2011 Intelygenz <www.intelygenz.com>
 * All rights reserved.
 * 
 * BSD License
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of copyright holders nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * Creation date: 2011-08-18
 * 
 * order 
 * [version] date (author)
 * 		upd|fix|new|dep - description
 *
 * [0.0.2] 2011-08-19 (angel.sanchez@intelygenz.com) 
 *     	new - Save - Create a new label in the navigation history (at depth desired).
 *      new - Last - Return the last navigation labels with label=p_label and depth=p_depth.
 *      new - Util.GetLabelByDepth - Return the label with depth=p_depth from current URL.
 * 
 * [0.0.1] 2011-08-18 (jonas.dacruz@intelygenz.com) 
 * 		new - first development version
 * 
 */

NavUri = new function (){

	// Full NavUri example
	// http://www.domain.com/folder/file.html#groups/group@xxx/docs/doc-preview@yyy
	
	var _History = [];

	/**
     * Create a new label in the navigation history (at depth desired)
     */
	this.Save = function (p_depth, p_label) {
		var newHistory;
		
		var fullUrl = document.location.href;
		var url = fullUrl.substr(0, fullUrl.indexOf("#"));

		
		if ( p_depth == 0 ) {
			newHistory = p_label;
		}
        else {
        	
        	var labels = document.location.hash.substr(1).split("/");
    		if ( labels.length >= p_depth ) {
    			newHistory = "";
    			var f = 0;
    			for ( f=0; f<p_depth; f++ ) {
    				newHistory += labels[f] + "/";
    			}
    			newHistory += p_label;
    		}
        }
		_History.push( newHistory );
		
		document.location.href = url + "#" + newHistory;
	};
    
    /** 
     * Return the last navigation labels with label=p_label and depth=p_depth.
     */
    this.Last = function ( p_depth, p_label ) {
        var lastUrl = "";
        
        var tagPos = lastUrl.indexOf("#");
        if ( tagPos > 0 ) {
            lastUrl = lastUrl.substr(0, tagPos);
        }
        
        var f = _History.length - 1;
        var historyMatched = null;
        var labels;
        while ( f >= 0 && historyMatched == null ) {
            labels = _History[f].split("/");
            
            if ( labels.length >= p_depth && labels[p_depth] == p_label ) {
                historyMatched = _History[f];
            }
            f--;
        }

        if ( historyMatched != null ) {
            lastUrl = historyMatched;
        }
        
        return lastUrl;
    }
    
    
    this.Util = new function (){

    	/** 
         * Return the label with depth=p_depth from current URL.
         * If the depth is not found, this will return ""
         */
    	this.GetLabelByDepth = function (p_depth){
    		
            var labels = document.location.hash.substr(1).split("/");
            
            var labelByDepth = "";
                
            if ( labels.length >= p_depth ) {
                labelByDepth = labels[p_depth];
            }
            
    		return labelByDepth;
    	};

    }

}
